package com.jdbc;

import java.sql.*;

/**
 * jdbc PreparedStatement
 */
public class Jdbc2_Login {
    public static void main(String[] args) throws Exception {
        // 1 . 注册驱动
        //Class.forName("com.mysql.jdbc.Driver");


        // 2. 获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/bd1?useSSL=false&useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "1234";
        Connection connection = DriverManager.getConnection(url, username, password);

        String name = "李四";
        String pswd = "1234";

        //sql
        String sql = "select * from tb_user where name = ? and password = ?";

        //传入sql 并获取preparedstatement 防止sql注入
        PreparedStatement pstet = connection.prepareStatement(sql);
        pstet.setString(1,name);
        pstet.setString(2,pswd);

        ResultSet resultSet = pstet.executeQuery();
        if(resultSet.next()){
            System.out.println("登陆成功");
        }else{
            System.out.println("登陆失败");
        }

        //7 .释放资源
        resultSet.close();
        pstet.close();
        connection.close();

    }
}
